package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lzj.pass.dialog.PayPassDialog;
import com.lzj.pass.dialog.PayPassView;
import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.AgreementBean;
import com.zlx.module_base.base_api.res_data.BaseInfoBean;
import com.zlx.module_base.base_api.res_data.BuyChargeRulesBean;
import com.zlx.module_base.base_api.res_data.BuyChargeRulesBody;
import com.zlx.module_base.base_api.res_data.ChargeRulesBean;
import com.zlx.module_base.base_api.res_data.QueryByBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_base.base_util.TimeUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.Constants;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.StringUt;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 会员中心
 */
public class MemberAc extends BaseAc {

    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.iv_vip)
    ImageView iv_vip;
    @BindView(R.id.rv_data1)
    RecyclerView rv_data1;
    @BindView(R.id.tv_time)
    TextView tv_time;

    private double money = 0;
    private double over = 0;
    //    private int videoCount;
    private String id = "";
    private BaseInfoBean mBaseInfoBean;
    private List<ChargeRulesBean.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<ChargeRulesBean.InfoDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_member;
    }

    @Override
    public void initViews() {
        super.initViews();

        mBaseInfoBean = (BaseInfoBean) getIntent().getSerializableExtra("data");

        setData();
        initRecyclerView();
        getChargeRules();
        queryById();
    }

    /**
     * 根据用户id查询个人信息
     */
    private void queryById() {
        ApiUtil.getLoginApi().queryById(SPUtil.getToken(this),(String) SPUtil.get(this, Constants.SP_ID, ""))
                .observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<QueryByBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<QueryByBean> data) {
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    if (null != data.getData().getInfo()) {
                                        if (data.getData().getInfo().size() > 0) {
                                            if (null != data.getData().getInfo().get(0).getResult()) {
                                                if (!StringUt.isEmpty(data.getData().getInfo().get(0).getResult().getMoney())) {
                                                    over = Double.valueOf(data.getData().getInfo().get(0).getResult().getMoney());
                                                }
                                            }
                                        }
                                    }
                                } else {
                                    Toast.makeText(MemberAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        }
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();

                    }
                }));
    }

    private void initRecyclerView() {
        rv_data1.setLayoutManager(new GridLayoutManager(this, 3));

        mAdapter = new CommonRecyclerViewAdapter<ChargeRulesBean.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, ChargeRulesBean.InfoDTO entity, int position) {
                ImageView iv_bg = holder.getView(R.id.iv_bg);
                TextView tv_text1 = holder.getView(R.id.tv_text1);
                TextView tv_text2 = holder.getView(R.id.tv_text2);
                tv_text1.setText(entity.getName());
                tv_text2.setText("￥" + entity.getMoney());
                if (entity.isFlag() == true) {
                    iv_bg.setImageResource(R.mipmap.member_bg_true);
                    tv_text1.setTextColor(getResources().getColor(R.color.white));
                    tv_text2.setTextColor(getResources().getColor(R.color.white));
                } else {
                    iv_bg.setImageResource(R.mipmap.member_bg_false);
                    tv_text1.setTextColor(getResources().getColor(R.color.black));
                    tv_text2.setTextColor(getResources().getColor(R.color.red));
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_member;
            }
        };

        rv_data1.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                for (int i = 0; i < mList.size(); i++) {
                    if (position == i) {
                        id = mList.get(position).getId();
                        if (!StringUt.isEmpty(mList.get(position).getMoney())) {
                            money = Double.valueOf(mList.get(position).getMoney());
                        }
                        mList.get(position).setFlag(true);
                    } else {
                        mList.get(i).setFlag(false);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void setData() {
        if (null != mBaseInfoBean) {
            if (null != mBaseInfoBean.getInfo()) {
                if (mBaseInfoBean.getInfo().size() > 0) {
                    if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getAvatar())) {
                        GlideImgManager.glideLoader(this, mBaseInfoBean.getInfo().get(0).getAvatar(), iv_head, 0);
                    }
                    if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getNickname())) {
                        tv_nickname.setText(mBaseInfoBean.getInfo().get(0).getNickname());
                    }
                    if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getIsvip())) {
                        if (mBaseInfoBean.getInfo().get(0).getIsvip().equals("1")) {
                            iv_vip.setVisibility(View.VISIBLE);
                        } else {
                            iv_vip.setVisibility(View.GONE);
                        }
                    } else {
                        iv_vip.setVisibility(View.GONE);
                    }
                    if (!StringUt.isEmpty(mBaseInfoBean.getInfo().get(0).getVipEndTime())) {
                        Date date = new Date(mBaseInfoBean.getInfo().get(0).getVipEndTime());
                        tv_time.setText(TimeUtil.simpleDateFormat("yyyy-MM-dd", date));
                    }
                }
            }
        }

    }

    private void getChargeRules() {
        ApiUtil.getHomeApi().getChargeRules(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<ChargeRulesBean>>() {
            @Override
            public void onSuccess(ApiResponse<ChargeRulesBean> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            if (null != data.getData().getInfo()) {
                                mList.addAll(data.getData().getInfo());
                            }
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));

    }

    @OnClick({R.id.tv_details, R.id.iv_back, R.id.ll_protocol, R.id.bt_clear, R.id.ll_help})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.bt_clear:
                //开通会员
                if (StringUt.isEmpty(id)) {
                    Toast.makeText(this, "请选择购买会员的套餐", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    dialogPay();
                }
                break;
            case R.id.ll_help:
                //充值帮助
                getAgreement("6");
                break;
            case R.id.ll_protocol:
                //会员服务协议
                getAgreement("5");
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_details:
                startActivity(new Intent(this, PurchaseDetailsAc.class));
                break;
        }
    }

    /**
     * 购买
     */
    private void dialogPay() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(this, R.layout.dialog_play_video, null);
        dialog.setContentView(view);

        TextView tv_money = view.findViewById(R.id.tv_money);
        tv_money.setText("￥" + money);

        TextView tv_over = view.findViewById(R.id.tv_over);

        ImageView iv_close = view.findViewById(R.id.iv_close);

        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        tv_over.setText("￥" + over);

        TextView bt_play = view.findViewById(R.id.bt_play);

        bt_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (over > money) {
                    //钱够支付
                    dialogPass();
                    dialog.dismiss();
                } else {
                    Toast.makeText(MemberAc.this, "余额不足", Toast.LENGTH_SHORT).show();
                }
            }
        });

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }

    /**
     * 支付密码
     */
    private void dialogPass() {
        final PayPassDialog dialog = new PayPassDialog(this);
        dialog.getPayViewPass()
                .setRandomNumber(true)
                .setPayClickListener(new PayPassView.OnPayClickListener() {
                    @Override
                    public void onPassFinish(String passContent) {
                        //6位输入完成回调
                        Log.d("ret", "joker  输入的密码===" + passContent);
                        BuyChargeRules(passContent);
                        dialog.dismiss();
                    }

                    @Override
                    public void onPayClose() {
                        dialog.dismiss();
                        //关闭弹框
                    }

                    @Override
                    public void onPayForget() {
                        dialog.dismiss();
                        //点击忘记密码回调
                    }
                });

    }


    private void BuyChargeRules(String pass) {
        BuyChargeRulesBody buyChargeRulesBody = new BuyChargeRulesBody();
        buyChargeRulesBody.setId(id);
        buyChargeRulesBody.setPassWord(pass);
        ApiUtil.getHomeApi().BuyChargeRules(
                SPUtil.getToken(this),
                buyChargeRulesBody
        ).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BuyChargeRulesBean>>() {
            @Override
            public void onSuccess(ApiResponse<BuyChargeRulesBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            //成功
                            Toast.makeText(MemberAc.this, "购买成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MemberAc.this,
                                    data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }));
    }

    private void getAgreement(String type) {
        ApiUtil.getHomeApi().getAgreement(type).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<AgreementBean>>() {
            @Override
            public void onSuccess(ApiResponse<AgreementBean> data) {
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            if (data.getData().getInfo().size() > 0) {
                                startActivity(new Intent(MemberAc.this, WebViewActivity.class)
                                        .putExtra("type", type)
                                        .putExtra("title", data.getData().getInfo().get(0).getPost_title())
                                        .putExtra("content", data.getData().getInfo().get(0).getPost_content())
                                );
                            }
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }
}
